<template>
  <div>
    <edu-dialog :title="$attrs.title" size="small" :visible.sync="show" :autoHeight="false"
      :confirmBtnText="$t('common.btn.confirm')" :close-on-click-modal="false" @close="close" @confirm="confirm">
      <el-form :model="condition" ref="addAuditBatchForm" :rules="rules" label-width="140px" label-position="right"
        size="small">
        <el-form-item :label="$t('auditBatch.graduationYear') + ':'" prop="graduateData">
          <el-date-picker
            v-model="condition.graduateData"
            value-format="yyyy"
            align="right"
            :disabled="!!$attrs.editData"
            type="year"
            :placeholder="$t('common.pla.pleaseSelect')"
            clearable
            style="width: 100%" />
        </el-form-item>
        <el-form-item :label="$t('auditBatch.auditBatch') + ':'" prop="reviewBatch">
          <el-select v-model="condition.reviewBatch" :placeholder="$t('common.pla.pleaseSelect')" :disabled="!!$attrs.editData" style="width: 100%"
            filterable clearable>
            <el-option v-for="item in options.auditBatch" :key="item.key" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('auditBatch.collegeReviewStartDate') + ':'" prop="collegeReviewStartDate">
          <el-date-picker v-model="condition.collegeReviewStartDate" :placeholder="$t('common.pla.pleaseSelect')"
            type="datetime" format="yyyy-MM-dd HH:mm" value-format="timestamp" align="right" style="width: 100%" />
        </el-form-item>
        <el-form-item :label="$t('auditBatch.collegeReviewEndDate') + ':'" prop="collegeReviewEndDate">
          <el-date-picker v-model="condition.collegeReviewEndDate" :placeholder="$t('common.pla.pleaseSelect')"
            type="datetime" format="yyyy-MM-dd HH:mm" value-format="timestamp" align="right" style="width: 100%" />
        </el-form-item>
        <el-form-item :label="$t('auditBatch.graduationCertificateDate') + ':'" prop="graduationCertificateDate">
          <el-date-picker v-model="condition.graduationCertificateDate" :placeholder="$t('common.pla.pleaseSelect')"
            type="date" format="yyyy-MM-dd" value-format="timestamp" align="right" style="width: 100%" />
        </el-form-item>
        <el-form-item :label="$t('auditBatch.leaveSchoolDate') + ':'" prop="leaveSchoolDate">
          <el-date-picker v-model="condition.leaveSchoolDate" :placeholder="$t('common.pla.pleaseSelect')"
            type="datetime" format="yyyy-MM-dd HH:mm" value-format="timestamp" align="right" style="width: 100%" />
        </el-form-item>
        <el-form-item :label="$t('auditBatch.degreeResolutionNo') + ':'" prop="degreeResolutionNo">
          <el-input v-model="condition.degreeResolutionNo" :placeholder="$t('common.pla.pleaseInput')" maxlength="256"></el-input>
        </el-form-item>
        <el-form-item :label="$t('auditBatch.useType') + ':'" :prop="status">
          <el-radio-group v-model="condition.status">
            <el-radio v-for="radio in options.useType" :key="radio.id" :label="radio.id">{{radio.value}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </edu-dialog>
  </div>
</template>
<script>
  import api from 'common/src/api/graduationManagement/graduationManage';
  export default {
    name: 'add-audit-batch',
    props: ['show'],
    data() {
      return {
        condition: {
          graduateData: '',
          reviewBatch: '',
          status: 1,
          collegeReviewStartDate: '',
          collegeReviewEndDate: '',
          graduationCertificateDate: '',
          leaveSchoolDate: '',
          degreeResolutionNo: ''
        },
        options: {},
        rules: {
          graduateData: [{
            required: true,
            message: this.$t('auditBatchValidate.graduationYear')
          }],
          reviewBatch: [{
            required: true,
            message: this.$t('auditBatchValidate.batch')
          }],
          collegeReviewStartDate: [{
            required: true,
            message: this.$t('auditBatchValidate.timeRange')
          }],
          collegeReviewEndDate: [{
            required: true,
            message: this.$t('auditBatchValidate.timeRange')
          }],
          graduationCertificateDate: [{
            required: true,
            message: this.$t('auditBatchValidate.graduationTime')
          }],
          leaveSchoolDate: [{
            required: true,
            message: this.$t('auditBatchValidate.levelShoolTime')
          }]
        },
        isOverseas: '0'
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        this.options = Object.assign(this.options, {
          auditBatch: this.$parent.options.auditBatch,
          useType: this.$parent.options.useType
        });
        if (this.$attrs.editData) {
          this.condition.graduateData = this.$attrs.editData.graduateData;
          this.condition.reviewBatch = this.$attrs.editData.reviewBatch;
          this.condition.status = this.$attrs.editData.status;
          this.condition.collegeReviewStartDate = this.dealDate(this.$attrs.editData.collegeReviewStartDate);
          this.condition.collegeReviewEndDate = this.dealDate(this.$attrs.editData.collegeReviewEndDate);
          this.condition.graduationCertificateDate = this.dealDate(this.$attrs.editData.graduationCertificateDate);
          this.condition.leaveSchoolDate = this.dealDate(this.$attrs.editData.leaveSchoolDate);
          this.condition.degreeResolutionNo = this.$attrs.editData.degreeResolutionNo;
        }
      },
      dealDate(time) {
        let timeStr = '';
        if (time) {
          time = time.replace(/-/g, '/');
          let newTime = new Date(time);
          timeStr = newTime.getTime();
        }
        return timeStr;
      },
      close() {
        this.$emit('updateShow');
      },
      confirm() {
        this.$refs.addAuditBatchForm.validate((valid) => {
          if (valid) {
            this.ok();
          } else {
            return false;
          }
        });
      },
      ok() {
        if (this.condition.collegeReviewStartDate >= this.condition.collegeReviewEndDate) {
          this.$message.error(this.$t('auditBatchValidate.aduitTimeRangeTip'));
          return;
        }
        let params = {
          ...this.condition,
          graduateData: this.condition.graduateData + '',
          isOverseas: this.isOverseas
        };
        let promise, msg;
        if (!this.$attrs.editData) {
          msg = this.$t('auditBatch.addBatchSuccessTip');
          promise = api.addGraduateReviewBatch(params);
        } else {
          params.id = this.$attrs.editData.id;
          msg = this.$t('auditBatch.editBatchSuccessTip');
          promise = api.updateGraduateReviewBatch(params);
        }
        promise.then(res => {
          if (res && res.code === 200) {
            this.$message({
              message: msg,
              type: 'success'
            }); 
          }
          this.$emit('updateShow');
          this.$parent.query();
        }).catch(error => {
          if (error && error.message) {
            this.$message({
            message: error.message,
            type: 'error'
          });
          }
        })

      }
    }
  }

</script>
<style lang="scss" scoped>
  ::v-deep .el-form-item .el-form-item__content {
    width: 320px;
  }

</style>
